package apis

import (
	"github.com/gin-gonic/gin"
	"go-admin/models"
	"go-admin/tools"
	"go-admin/tools/app"
	"go-admin/tools/app/msg"
)

func GetZxRevisitList(c *gin.Context) {
	var data models.VRRevisit
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	data.OrderId = tools.ParseFormStr(c, "orderId")
	data.RevisiterId = tools.ParseFormInt(c, "revisiterId")
	data.CreateBy = tools.GetUserIdStr(c)
	data.RevisitState = tools.ParseFormStr(c, "revisitState")
	//data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetRevisitPage(pageSize, pageIndex)
	tools.HasError(err, "", -1)

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetZxRevisit(c *gin.Context) {
	var data models.VRRevisit
	revisitId := c.Param("id")
	result, err := data.GetRevisitById(revisitId)
	if err != nil {
		app.ErrMsg(c, err.Error())
		return
	}
	//tools.HasError(err, "抱歉未找到相关信息", -1)

	app.OK(c, result, "")
}

func InsertZxRevisit(c *gin.Context) {
	var data models.ZxRevisit
	err := c.ShouldBindJSON(&data)
	data.CreateBy = tools.GetUserIdStr(c)
	tools.HasError(err, "", 500)
	result, err := data.Create()
	tools.HasError(err, "", -1)
	app.OK(c, result, "")
}

func UpdateZxRevisit(c *gin.Context) {
	var data models.VRRevisit
	err := c.ShouldBindJSON(&data)
	tools.HasError(err, "数据解析失败", -1)
	data.UpdateBy = tools.GetUserIdStr(c)
	result, err := data.Update()
	//result, err := data.UpdateVRRevisit()
	tools.HasError(err, "", -1)

	app.OK(c, result, "")
}

//func UpdateZxRevisit(c *gin.Context) {
//	var data models.ZxRevisit
//	err := c.BindWith(&data, binding.JSON)
//	tools.HasError(err, "数据解析失败", -1)
//	data.UpdateBy = tools.GetUserIdStr(c)
//	result, err := data.Update(data.Id)
//	tools.HasError(err, "", -1)
//
//	app.OK(c, result, "")
//}

func DeleteZxRevisit(c *gin.Context) {
	var data models.ZxRevisit
	data.UpdateBy = tools.GetUserIdStr(c)

	IDS := tools.IdsStrToIdsIntGroup("id", c)
	_, err := data.BatchDelete(IDS)
	tools.HasError(err, msg.DeletedFail, 500)
	app.OK(c, nil, msg.DeletedSuccess)
}
